//
//  VoiceChatViewController.swift
//  FloatingWindowExample
//
//  Created by Jason on 2021/8/12.
//

import UIKit
import SwiftyFloatingWidget

class VoiceChatViewController: UIViewController {

    
    deinit {
        print("界面释放测试")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .black
        
        view.addSubview(backImageView)
        backImageView.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.top.equalToSuperview()
            make.width.equalTo(ScreenWidth)
            make.height.equalTo(ScreenHeight)
        }
        
        view.addSubview(avatarImageView)
        avatarImageView.snp.makeConstraints { (make) in
            make.width.height.equalTo(avatarImageViewWidth)
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(-70)
        }
        
        view.addSubview(talkDateLabel)
        talkDateLabel.snp.makeConstraints { (make) in
            make.height.equalTo(20)
            make.centerX.equalToSuperview()
            make.top.equalTo(avatarImageView.snp.bottom).offset(30)
        }
        
        setupCustomNavigationBar()
        
        startTimer()
    }
    
    fileprivate func setupCustomNavigationBar(){
        
        view.addSubview(ownNavigationBar)
        ownNavigationBar.snp.makeConstraints { (make) in
            make.right.left.top.equalTo(view)
            make.height.equalTo(statusBarHeight+44)
        }
        
        ownNavigationBar.addSubview(leftNavBtn)
        ownNavigationBar.addSubview(rightNavBtn)
        
        leftNavBtn.snp.makeConstraints { (make) in
            make.left.equalTo(ownNavigationBar).offset(8)
            make.bottom.equalTo(ownNavigationBar)
            make.width.height.equalTo(44)
        }
        
        rightNavBtn.snp.makeConstraints { (make) in
            make.right.equalTo(ownNavigationBar).offset(-4)
            make.bottom.equalTo(ownNavigationBar)
            make.height.equalTo(44)
            make.width.equalTo(66)
        }
    }
    
    fileprivate func startTimer(){
        
        timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(fireTimer), userInfo: nil, repeats: true)
        
    }
    
    @objc fileprivate func fireTimer() {
        
        seconds += 1
        
        let str = secondsToHoursMinutesSecondsString(seconds: seconds)
        
        talkDateLabel.text = str
        
        suspendCountdownView.bind(seconds: str)
    }
    
//    MARK: - Properties
    fileprivate var seconds = 0
    
    
    fileprivate lazy var backImage = UIImage.init(named: "Voice_chat_background")
    fileprivate lazy var backImageView:UIImageView = {
       let control = UIImageView.init(image: backImage)
       return control
   }()
    
    fileprivate lazy var avatarImageViewWidth:CGFloat = 128
    
    fileprivate lazy var avatarImageView:UIImageView = {
        let control = UIImageView.init(image: UIImage(named: "avatar"))
       control.contentMode = .scaleAspectFill
       control.cornerRadius = self.avatarImageViewWidth/2.0
       control.borderColor = .white
       control.borderWidth = 2
       return control
   }()
    
    fileprivate lazy var talkDateLabel:UILabel = {
        let control = UILabel.init()
        control.textColor = UIColor.white
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        control.text = "00:00"
        return control
    }()
    
    fileprivate lazy var ownNavigationBar:UIView = {
        let control = UIView()
        control.backgroundColor = .clear
        return control
    }()
    
    
    fileprivate lazy var leftNavBtn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        
        control.setImage(UIImage.init(named: "suspend_icon"), for: UIControl.State.normal)
    
        control.imageView?.contentMode = UIView.ContentMode.scaleAspectFit
        
        control.addTarget(self, action: #selector(leftClick), for: UIControl.Event.touchUpInside)
        
        return control
    }()
    
    fileprivate lazy var rightNavBtn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setTitleForAllStates("挂断")
        control.setTitleColorForAllStates(#colorLiteral(red: 1, green: 1, blue: 1, alpha: 1))
        control.cornerRadius = 8
        control.backgroundColor = #colorLiteral(red: 0.2745098174, green: 0.4862745106, blue: 0.1411764771, alpha: 1)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        control.addTarget(self, action: #selector(rightClick), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    
//    MARK: 共享View
    fileprivate lazy var flyView:UIView = {
       let control = UIView()
        control.addSubview(suspendCountdownView)
        suspendCountdownView.snp.makeConstraints { make in
            make.width.height.equalTo(62)
            make.center.equalToSuperview()
        }
       return control
   }()
    
    fileprivate lazy var suspendCountdownView:ShareCountdownView = {
        let control = ShareCountdownView()
        control.bind(seconds: "00:00")
        return control
    }()
    
    fileprivate var timer:Timer?
    
}

extension VoiceChatViewController{
    
    @objc fileprivate func leftClick(){
        
        let config = SwiftyWidgetConfig(animationDuration: 0.25, FloatingDefaultPoint: CGPoint(x: 0, y: 100), FloatingDefaultSize: CGSize(width: 78, height: 80), FloatingWindowRadious: 14, windowContentEdge: UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8), windowBackgroundColor: #colorLiteral(red: 0.9411764706, green: 0.9411764706, blue: 0.9411764706, alpha: 1))
        
        SwiftyFloatingWidget.provider.floating(config: config, controller: self)
        
    }
    
    @objc fileprivate func rightClick(){
        
        timer?.invalidate()
        timer = nil
        
        self.dismiss(animated: true, completion: nil)
        
    }
    
    
}

//MARK: - SwiftyFloatingWidgetProtocol
extension VoiceChatViewController:SwiftyFloatingWidgetProtocol{
    
    func swiftyFloatingShareView() -> UIView {
        return flyView
    }
}


//MARK: - Other

let ScreenHeight:CGFloat = {
    return UIScreen.main.bounds.height
}()

let ScreenWidth:CGFloat = {
    return UIScreen.main.bounds.width
}()

let statusBarHeight:CGFloat = {

   var statusBarHeight: CGFloat = 0
   if #available(iOS 13.0, *) {
       let window = UIApplication.shared.windows.filter {$0.isKeyWindow}.first
       statusBarHeight = window?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0
   } else {
       statusBarHeight = UIApplication.shared.statusBarFrame.height
   }
   return statusBarHeight
}()


func secondsToHoursMinutesSeconds (seconds : Int) -> (hour:Int, minute:Int, second:Int) {
    return (hour:seconds / 3600, minute:(seconds % 3600) / 60, second:(seconds % 3600) % 60)
}

func secondsToHoursMinutesSecondsString(seconds:Int) -> String {
    
    let date = secondsToHoursMinutesSeconds(seconds: seconds)
    
    let hours = date.hour
    
    let minutes = date.minute
    
    let seconds = date.second
    
    if hours > 0{
        return String(format: "%02d:%02d:%02d", hours,minutes,seconds)
    }else{
        return String(format: "%02d:%02d", minutes,seconds)
    }
}
